package itdep.common.resolver;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Ivan Khalopik
 * @version $Revision: 4 $ $Date: 2010-06-10 13:13:16 +0000 (Thu, 10 Jun 2010) $
 */
public class SimpleResolver<K, V> implements Resolver<K, V> {
	private final Map<K, V> source = new HashMap<K, V>();

	public void setSource(final Map<K, V> source) {
		this.source.putAll(source);
	}

	public void setSource(final Collection<V> source) {
		for (V value : source) {
			addToSource(value);
		}
	}

	@SuppressWarnings({"unchecked"})
	protected void addToSource(final V value) {
		if (value != null && value instanceof Resolvable) {
			source.put(((Resolvable<K>) value).getResolveKey(), value);
		}
	}

	public V resolve(final K key) {
		return source.get(key);
	}
}
